<template>
  <el-dialog class="dia-style" :title="title" :visible.sync="open" :close-on-click-modal="false">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-col :span="24">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入字典名称" maxlength="255" />
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="字典编码" prop="code">
          <el-input v-model="formData.code" placeholder="请输入字典编码" maxlength="255" />
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="formData.sort" :min="0" controls-position="right" />
        </el-form-item>
      </el-col>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addDictClassify, updateDictClassify } from '@/api/milkyway/dictManager'

export default {
  props: {
    title: {
      type: String,
      default() {
        return '标题'
      }
    },
    form: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      // 是否显示弹出层
      open: false,
      // 标题宽度
      labelWidth: '100',
      // 是否显示按钮
      btnHidden: true,
      // 表单项配置

      // 表单校验
      rules: {
        name: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入字典编码', trigger: 'blur' }],
        sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    formData: {
      get() {
        return this.form
      },
      set(val) {}
    }
  },
  watch: {},
  created() {},
  methods: {
    show() {
      this.open = true
      this.reset()
      this.$nextTick(() => {
        this.$refs.formRef.clearValidate()
      })
    },
    /** 提交按钮 */
    async submitForm() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          if (this.title === '更新字典分类') {
            updateDictClassify(this.formData).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.$emit('handleSubmit')
            })
          } else {
            addDictClassify(this.formData).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.$emit('handleSubmit')
            })
          }
        } else {
          this.$message.error('请完善表单')
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.formData = {
        id: undefined,
        code: undefined,
        createDate: undefined,
        flag: undefined,
        isDeleted: undefined,
        modifiedDate: undefined,
        name: undefined,
        sort: undefined
      }
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep {
  .el-dialog:not(.is-fullscreen) {
    height: 400px !important;
  }
  .el-dialog__body {
    height: auto !important;
  }
  .el-dialog__footer {
    position: absolute;
    bottom: 0;
  }
}
</style>
